<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\User;
use App\Models\WorkAttendanceMonthRecord;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class WorkAttendanceMonthRecordController extends ApiController
{

    public function show(Request $request) {
        $work_attendance_month = WorkAttendanceMonthRecord::findOrFail($request->id);
        $work_attendance_month->employee_info = Employee::where('employee_id',$work_attendance_month->employee_id)->where('company_id',$work_attendance_month->company_id)->where('status',1)->first();
        return showError(Error::ERR_OK,['work_attendance_month'=>$work_attendance_month]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = WorkAttendanceMonthRecord::getList($where);
        foreach ($list as $k => &$v) {

            $v->employee_info = Employee::where('employee_id',$v->employee_id)->where('company_id',$company_id)->where('status',1)->first();
        }
        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        WorkAttendanceMonthRecord::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
